//logs.js
const util = require('../../utils/util.js')
let end = false

Page({
  data: {
    currentIndex:0,
    numShow:true,
    cartnum:1,
    lists: [
      {
        'name':'今日新品',
        'page': 1,
        'end': false,
        'data': [{
          id: 1,
          image:'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg',
          info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
          popularity: '6.7万',
          presaletime: 1564624526889,
          deliverytime: 1564624526889,
          limitnum: 2500,
          soldnum:2000,
          status:1,//提前加入购物车
          begintime: 1564624526889,
          buynum: '1.2万',
          price: 14.99,
          oldprice: 16,
        },
        {
          id: 2,
          image:'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg',
          info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
          popularity: '6.7万',
          presaletime: 1564624526889,
          deliverytime: 1564624526889,
          limitnum: 2500,
          soldnum:2000,
          status:0,//加入购物车
          buynum: '1.2万',
          price: 14.99,
          oldprice: 16,
        },
        {
          id: 3,
          image:'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg',
          info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
          popularity: '6.7万',
          presaletime: 1564624526889,
          deliverytime: 1564624526889,
          limitnum: 2500,
          soldnum:2000,
          status:1,//提前加入购物车
          begintime: 1564624526889,
          buynum: '1.2万',
          price: 14.99,
          oldprice: 16,
        }
      ]
      },
      {
        'name':'水果馆',
        'page': 1,
        'end': false,
        'data': [{
          id: 1,
          image:'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg',
          info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
          popularity: '6.7万',
          presaletime: 1564624526889,
          deliverytime: 1564624526889,
          limitnum: 2500,
          soldnum:2000,
          status:1,//提前加入购物车
          begintime: 1564624526889,
          buynum: '1.2万',
          price: 13.99,
          oldprice: 17,
        }]
      }
    ],
    data1: [{
      id: 4,
      image:'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg',
      info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
      popularity: '6.7万',
      presaletime: 1564624526889,
      deliverytime: 1564624526889,
      limitnum: 2500,
      soldnum:2000,
      status:1,//提前加入购物车
      begintime: 1564624526889,
      buynum: '1.2万',
      price: 144.99,
      oldprice: 16,
    }],
    leftMenuList: []
  },
  onLoad(){
    const lists = [...this.data.lists]
    let leftMenuList = lists.map(item=>{
      return item.name
    })
    for(let i=0;i<lists.length;i++){
        for(let j=0;j<lists[i].data.length;j++){
          lists[i].data[j].presaletime = this.getMD(lists[i].data[j].presaletime)
          lists[i].data[j].deliverytime = this.getMD(lists[i].data[j].deliverytime)
        }
    }
    this.setData({
      leftMenuList,
      lists
    })
  },
  addcart(){
    wx.setTabBarBadge({
      index: 2,
      text:'1'
     });
      this.setData({
        numShow:false
      })
  },
  handleItemTap(e) {
    console.log(e)
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  onReachBottom: function (e) {
    console.log('onReachBottom')
    const lists = this.data.lists
    const data1 = this.data.data1
    if(lists[this.data.currentIndex].end){
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    for(let i=0;i<data1.length;i++){
      data1[i].presaletime = this.getMD(data1[i].presaletime)
      data1[i].deliverytime = this.getMD(data1[i].deliverytime)
    }
    lists[this.data.currentIndex].data = [...lists[this.data.currentIndex].data,...data1]
    lists[this.data.currentIndex].end=true
    this.setData({
      lists
    })
    wx.hideLoading({})
  },
  getMD(timestamp, hour) {
    if (hour) {
      let time = new Date(timestamp)
      let month = time.getMonth() + 1
      let date = time.getDate()
      let hours = time.getHours()
      if (month < 10) {
        month = '0' + month
      }
      if (date < 10) {
        date = '0' + date
      }
      if (hours < 10) {
        hours = '0' + hours
      }
      return month + '月' + date + '日' + hours + '点'
    }
    let time = new Date(timestamp)
    let month = time.getMonth() + 1
    let date = time.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (date < 10) {
      date = '0' + date
    }
    return month + '月' + date + '日'
  },
  add(){
    let cartnum = this.data.cartnum+1
    this.setData({
      cartnum
    })
    let num = cartnum+''
    wx.setTabBarBadge({
      index: 2,
      text:num
     });
  },
  decrease(){
    let cartnum = this.data.cartnum-1
    this.setData({
      cartnum
    })
    let num = cartnum+''
    wx.setTabBarBadge({
      index: 2,
      text:num
     });
     if(cartnum==0){
      this.setData({
        numShow:true,
        cartnum:1
      })
      wx.removeTabBarBadge({
        index: 2,
        text: ''
    })
    }
  }
})